﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using ActivitySystem.DataContract;

namespace ActivitySystem.DataAccess
{
    public partial class ActivityDao
    {
        public int SearchCount(string type, string title, string launcher, DateTime date)
        {
            int count = 0;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Activity_Search_Count";
            command.Parameters.Add(new SqlParameter("@Type", type));
            command.Parameters.Add(new SqlParameter("@Title", title));
            command.Parameters.Add(new SqlParameter("@Launcher", launcher));
            command.Parameters.Add(new SqlParameter("@Date", date));

            try
            {
                connection.Open();

                count = (int)command.ExecuteScalar();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return count;
        }

        public Collection<Guid> Search(string type, string title, string launcher, DateTime date, int pageIndex, int pageSize)
        {
            Collection<Guid> activityIds = new Collection<Guid>();

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Activity_Search";
            command.Parameters.Add(new SqlParameter("@Type", type));
            command.Parameters.Add(new SqlParameter("@Title", title));
            command.Parameters.Add(new SqlParameter("@Launcher", launcher));
            command.Parameters.Add(new SqlParameter("@Date", date));
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Guid id = reader.GetGuid(0);
                        activityIds.Add(id);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return activityIds;
        }

        #region Associated Entity Related

        const string Participation_ColumnName = "ActivityId";
        const string Participation_TableName = "TR_Participation";
        const string Participation_ValueColumnName = "Id";

        const string ActivityType_ColumnName = "ActivityId";
        const string ActivityType_TableName = "TR_Activity_ActivityType";
        const string ActivityType_ValueColumnName = "TypeId";

        public void FillEntity(Activity activity)
        {
            DaoHelper.FillEntity<Guid>(activity.Participations, activity.Id, Participation_ColumnName, Participation_TableName, Participation_ValueColumnName);
            DaoHelper.FillEntity<Guid>(activity.ActivityTypes, activity.Id, ActivityType_ColumnName, ActivityType_TableName, ActivityType_ValueColumnName);
        }

        public void SaveEntity(Activity activity)
        {
            TransactionHelper.ProcessTransaction
            (() =>
            {
                DaoHelper.RemoveEntity(activity.Id, ActivityType_ColumnName, ActivityType_TableName);
                DaoHelper.InsertEntity<Guid>(activity.ActivityTypes, activity.Id, ActivityType_ColumnName, ActivityType_TableName, ActivityType_ValueColumnName);
            }
            );
        }

        #endregion
    }
}
